<?php

class Model extends db {
	public $select;
	public $where;
	public $order;
	public $limit;
	
	public function __construct()
	{
		$this->connect();
	}
	public function __destruct()
	{
		$this->close();
	}
	
	public function select($str){
		$this->select=$str;
	}
	
	public function order($str){
		$this->order=$str;
	}
	
	public function where($data)
	{
		if(is_array($data))
		{	
			foreach($data as $key => $value)
			{	
				$value=mysql_real_escape_string($value);
				$val[]="$key=\"$value\"";	
			}
			$this->where=implode(" and ",$val);
			
		}
		else
		{
			$this->where=$data;
		}
	}
	
	
	public function insert($table,$data)
	{
		$col=implode(",",array_keys($data));
		$val=array_values($data);
		foreach($val as $value)
		{	$value=mysql_real_escape_string($value);
			$val_array[]="\"$value\"";
		}
		$val2=implode(",",$val_array);
		$sql="insert into $table($col) values($val2)";
		if($this->nonquery($sql))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
		public function update($table,$data){
		if($this->where){
			$where="where $this->where";
		}
		foreach($data as $k=>$v){
			$v=mysql_real_escape_string($v);
			$arr[]="$k = \"$v\"";
		}
		$str=implode(",",$arr);
		$sql="update $table set $str $where";
		return $this->nonquery($sql);
	}
		public function delete($table){
		if($this->where){
			$where="where $this->where";
		}

		$sql="delete from $table $where";
		$this->nonquery($sql);
	}
	public function getdata($table){
		if($this->select){
			$select=$this->select;
		}else{
			$select="*";
		}
		if($this->where){
			$where="where $this->where";
		}
		if($this->order){
			$order="order by $this->order";
		}
		if($this->limit){
			$limit = "limit $this->limit";
		}
		$sql="select $select from $table $where $order $limit";
			$this->query($sql);
	}
	
}



?>